#include<stdio.h>
#include<stdarg.h>
#include <string.h>


int pattern(char*fmt,char*result,int index,int*start,int*end){
	char temp=0;
	int fmtStart=-1;
	do{
		temp=fmt[index];
		index++;
		if(fmtStart>=0&&(temp=='d'||temp=='s'||temp=='x'||temp=='X')){
			result[fmtStart]=temp;
			result[fmtStart+1]=0;
			*end=index-1;
			break;
		}
		if(temp=='%'){
			*start=index-1;
			fmtStart=0;
		}
		if(fmtStart>=0){
			result[fmtStart]=temp;
			fmtStart++;
		}

	}while(temp!=0);
	return fmtStart;
}

int main(){
	char *str = "%dHello World number:%d,address:%s age:%02d   \r\n";
	char buf[20]={0};
	int index=0;
	int start;
	int end;
	printf("%s\r\n",str);
	printf("-------start------\r\n");
	char temp[1000]={0};
	while(1){
		int ret=pattern(str,buf,index,&start,&end);
		if(ret<0){
			break;
		}
		printf("index:%d,start:%d,end:%d\r\n",index,start,end);
		memset(temp,0,1000);
		strncpy(temp,str+index,(start-index));
		printf("**** %s\r\n",temp);
		printf("fmt:%s\r\n",buf);
		index=end+1;

	}
	//printf("result:%d %s,start:%d end:%d\r\n",ret,buf,start,end);

	return 0;
}
